//
//  LongriseAppDelegate.m
//  HttpDemo
//
//  Created by 程三 on 17/5/27.
//  Copyright © 2017年 程三. All rights reserved.
//

#import "LongriseAppDelegate.h"
//#import "IQKeyboardManager.h"
#import "Util.h"
#import "PermissionManager.h"

#define LongriseAppNotificationKey @"isFirst"
#define LongriseIMMsgNoticeName @"LongriseIMMsgNoticeName"
#define LongriseIMMsgNoticeTokenSuccess @"LongriseIMMsgNoticeTokenSuccess"
#define LongriseIMMsgNoticeTokenFail @"LongriseIMMsgNoticeTokenFail"

@implementation LongriseAppDelegate


/** 消息推送注册 */
-(void)registerNotification:(UIApplication *)application
{
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >8.0)
    {
        //iOS8 - iOS10
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
        
    }
    else
    {
        //iOS8系统以下
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
    }
    // 注册获得device Token
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    
    /*
    //首次的话直接注册
    //判断是非是第一次
    NSInteger result = (NSInteger)[Util getDataForKey:LongriseAppNotificationKey];
    if(0 == result)
    {
        if ([UIDevice currentDevice].systemVersion.floatValue > 8.0)
        {
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
            [application registerUserNotificationSettings:settings];
            [application registerForRemoteNotifications];
        }
        else
        {
            [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
        }
        
        [Util saveNSUserDefaultsForInteger:1 forKey:LongriseAppNotificationKey];
    }
    else
    {
        [[PermissionManager getPermissionManager] requestNotificationPermissionBlock:^(BOOL b) {
            
        }];
    }
    */
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _getTokenType = -1;
    
    //启用键盘
//    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
//    manager.enable = YES;
//    manager.enableAutoToolbar = YES;

    return YES;
}


#pragma mark 会接收来自苹果服务器给你返回的deviceToken，然后你需要将它添加到你本地的推送服务器上。（很重要，决定你的设备能不能接收到推送消息）。
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    /*
     // 方式1
     NSMutableString *deviceTokenString1 = [[NSMutableString alloc] init];
     const char *bytes = deviceToken.bytes;
     int iCount = (int)deviceToken.length;
     for (int i = 0; i < iCount; i++) {
     [deviceTokenString1 appendFormat:@"%02x", bytes[i]&0x000000FF];
     }
     NSLog(@"方式1：%@", deviceTokenString1);
     
    // 方式2
    NSString *deviceTokenString2 = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
                                     stringByReplacingOccurrencesOfString:@">" withString:@""]
                                    stringByReplacingOccurrencesOfString:@" " withString:@""];
     */
    
    NSString *deviceTokenString2 = [Util saveDeviceToken:deviceToken];
    if(deviceTokenString2)
    {
        _getTokenType = 1;
    }
    //获取Token成功的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LongriseIMMsgNoticeTokenSuccess object:nil userInfo:nil];

    NSLog(@"deviceTokenString2:%@",deviceTokenString2);
 }

/** 设备接收到来自苹果推送服务器的消息时触发的,用来显示推送消息 */

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    /* eg.
     key: aps, value: {
     alert = "\U8fd9\U662f\U4e00\U6761\U6d4b\U8bd5\U4fe1\U606f";
     badge = 1;
     sound = default;
     }
     */

    //角标清零
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    //接收到推送消息发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LongriseIMMsgNoticeName object:nil userInfo:userInfo];
    NSLog(@"userInfo:%@",userInfo);
    
    
}

/** 注册推送服务失败 */
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    //判断是非有权限
    [[PermissionManager getPermissionManager] requestNotificationPermissionBlock:^(BOOL b) {
        
    }];
    
    _getTokenType = 0;
    //获取Token失败的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LongriseIMMsgNoticeTokenFail object:nil userInfo:nil];
    NSLog(@"注册推送服务失败");
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    return false;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return false;
}

@end
